#include<vector>
#include<iostream>
#include<algorithm>
using std::sort;
using std::max;
using std::vector;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> result = {intervals[0]};
        for (int i = 1; i < intervals.size(); i++) {
            if (intervals[i][0] > result[result.size()-1][1]) {
                result.push_back(intervals[i]);
            }
            else {
                result[result.size()-1][1] = max(intervals[i][1], result[result.size()-1][1]);
            }
        }
        return result;
    }
};